package com.yz.group5.med_booking.controller;

import com.yz.group5.med_booking.entity.Department;
import com.yz.group5.med_booking.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin //允许跨域请求
//接受请求，返回结果
@RestController //标记当前类是一个控制器类  返回结果为JSON数据结构
@RequestMapping("/department") //统一的前缀，前端访问 /department  开头的接口
public class DepartmentController {

//    找Service 要数据
    @Autowired //注入Service,从容器中拿departmentService实现类对象
//    DepartmentServiceImpl
    private DepartmentService departmentService;
//    api:application+program+interface 应用程序编程接口
    @GetMapping// 对应的是Get请求，访问"/department"路径时触发此api
    public List<Department> departmentsList() {
        return departmentService.listAllDepartment();//老师写的

//        List<Department> departments = departmentService.listAllDepartment();
//        // 遍历数据，给每个部门设置新的序号（从1开始）
//        for (int i = 0; i < departments.size(); i++) {
//            Department department = departments.get(i);
//            // 假设你有 DepartmentDTO 用于前端交互，新增一个 `displayId` 字段存序号
//            department.setId(i + 1);
//        }
//        return departments;
        //豆包写的
    }
    //api:application+program+interface 应用程序编程接口
    //对应的是Post请求，访问“/department/updateStatus”路径时触发此Api
    @PostMapping("/updateStatus")
    public boolean updateStatus(@RequestBody Department department) {
        //Department department :接收传递过来的参数 id，status
        return departmentService.updateById(department);
    }
    //对应是Delete请求，访问"/delete/{id}"路径时触发此Api
    //@PathVariable 获取访问路径中的变量
    @DeleteMapping("{id}") // 路径参数接收科室ID
    public boolean deleteDepartment(@PathVariable Integer id) {
        // 调用Service层的删除方法，返回删除结果（true/false）
        return departmentService.removeById(id);
    }
    //api:application+program+interface 应用程序编程接口
    //对应的是Post请求，访问“/department/add”路径时触发此Api
    @PostMapping("/add")
    public boolean add(@RequestBody Department department) {
        //Department department :接收传递过来的参数
        return departmentService.save(department);
    }


}
